﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoPlayer.Entity
{
    public static class DbManager
    {
        public static List<PlaylistItemDb> GetList()
        {
            return DbContext.Db.Queryable<PlaylistItemDb>().ToList();
        }

        public static PlaylistItemDb Get(int id)
        {
            return DbContext.Db.Queryable<PlaylistItemDb>().First(p => p.id == id);
        }

        public static PlaylistItemDb GetByHash(string hash)
        {
            return DbContext.Db.Queryable<PlaylistItemDb>().First(p => p.hash == hash);
        }

        public static PlaylistItemDb GetNew(List<string> hashList)
        {
            return DbContext.Db.Queryable<PlaylistItemDb>().First(p => !hashList.Contains(p.hash));
        }

        public static string GetPath(int id)
        {
            string path = string.Empty;
            var item = Get(id);
            if (item is not null)
            {
                path = item.LocalPath;
            }
            return path;
        }

        public static void Update(PlaylistItemDb item)
        {
            var x = DbContext.Db.Storageable(item).ToStorage(); //将数据进行分组 
            x.AsInsertable.ExecuteCommand(); //执行插入
            x.AsUpdateable.ExecuteCommand(); //执行更新　
        }
    }
}
